package com.jjclub.service.impl.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jjclub.entity.JjclubRole;
import com.jjclub.entity.JjclubRoleMenu;
import com.jjclub.mapper.JjclubRoleMapper;
import com.jjclub.mapper.JjclubRoleMenuMapper;
import com.jjclub.service.system.RoleService;
import com.jjclub.util.CommonUtil;
import com.jjclub.vo.ResponseVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private JjclubRoleMapper jjclubRoleMapper;


    @Autowired
    private JjclubRoleMenuMapper jjclubRoleMenuMapper;
    @Override
    public ResponseVO roleList(Integer nowPage,Integer pageSize) {
        PageHelper.startPage(nowPage, pageSize);
        List<JjclubRole> roles = jjclubRoleMapper.getAllRoles();
        PageInfo<JjclubRole> page = new PageInfo<>(roles);
        ResponseVO responseVO = new ResponseVO();
        responseVO.setMsg("success");
        responseVO.setCode(0);
        responseVO.setCount(page.getTotal());
        responseVO.setData(page.getList());
        return responseVO;
    }

    @Transactional
    @Override
    public ResponseVO RoleAdd(JjclubRole jjclubRole) {
        ResponseVO responseVO = new ResponseVO();
        String key  = CommonUtil.getKey();
        jjclubRole.setCreatetime(new Date());
        jjclubRole.setId(key);
        jjclubRoleMapper.insert(jjclubRole);
        String menus = jjclubRole.getMenus();
        if (CommonUtil.isNotBlank(menus)){
            String[] strings = menus.split(",");
            for (String s:strings){
                JjclubRoleMenu jjclubRoleMenu = new JjclubRoleMenu();
                jjclubRoleMenu.setMenuid(s);
                jjclubRoleMenu.setRoleid(key);
                jjclubRoleMenuMapper.insert(jjclubRoleMenu);
            }
        }
        responseVO.setCode(0);
        responseVO.setMsg("添加成功");
        return responseVO;
    }

    @Override
    public JjclubRole getJjclubRoleByid(String roleid) {
        return jjclubRoleMapper.selectByPrimaryKey(roleid);
    }

    @Transactional
    @Override
    public ResponseVO roleDel(String id) {
        ResponseVO responseVO = new ResponseVO();
        jjclubRoleMapper.deleteByPrimaryKey(id);
        jjclubRoleMenuMapper.delteMenusRoleByRoleid(id);
        responseVO.setCode(0);
        responseVO.setMsg("删除成功");
        return responseVO;
    }
    @Transactional
    @Override
    public ResponseVO editRole(JjclubRole jjclubRole) {
        ResponseVO responseVO = new ResponseVO();
        jjclubRoleMenuMapper.delteMenusRoleByRoleid(jjclubRole.getId());
        jjclubRole.setCreatetime(new Date());
        jjclubRoleMapper.updateByPrimaryKey(jjclubRole);
        String menus = jjclubRole.getMenus();
        if (CommonUtil.isNotBlank(menus)){
            String[] strings = menus.split(",");
            for (String s:strings){
                JjclubRoleMenu jjclubRoleMenu = new JjclubRoleMenu();
                jjclubRoleMenu.setMenuid(s);
                jjclubRoleMenu.setRoleid(jjclubRole.getId());
                jjclubRoleMenuMapper.insert(jjclubRoleMenu);
            }
        }
        responseVO.setCode(0);
        responseVO.setMsg("修改成功");
        return responseVO;
    }

    @Override
    public List<JjclubRole> getRoles() {
        return jjclubRoleMapper.getAllRoles();
    }
}
